import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/widgets/flutter_switch.dart';

//medium16高度，large24高度
enum GTSwitchSizeV3 { medium, large }

class GTSwitchV3 extends StatefulWidget {
  final bool? switchValue;
  final ValueChanged<bool>? onChanged;
  final bool? switchEnabled;
  final GTSwitchSizeV3 switchSize;

  const GTSwitchV3(
      {super.key,
      this.switchValue,
      this.onChanged,
      this.switchEnabled = true,
      this.switchSize = GTSwitchSizeV3.medium});

  @override
  State<StatefulWidget> createState() => _SwitchStateV3();
}

class _SwitchStateV3 extends State<GTSwitchV3> {
  bool _switchValue = false;

  @override
  Widget build(BuildContext context) {
    _switchValue = widget.switchValue ?? false;

    //背景色
    Color getActiveColor() {
      if (widget.switchEnabled == true) {
        return GTColorsV3.brand1.color;
      } else {
        return GTColorsV3.cmpt18.color;
      }
    }

    Color getInactiveColor() {
      if (widget.switchEnabled == true) {
        return GTColorsV3.neutral3.color;
      } else {
        return GTColorsV3.neutral1.color;
      }
    }

    //滑块颜色
    Color getToggleColor() {
      if (widget.switchEnabled == true) {
        return GTColorsV3.icon6.color;
      } else {
        if(_switchValue){
          return GTColorsV3.cmpt17.color;
        }else{
          return GTColorsV3.divider4.color;
        }
      }
    }

    double getSwitchWidth() {
      if (widget.switchSize == GTSwitchSizeV3.medium) {
        return 32;
      } else {
        return 48;
      }
    }

    double getSwitchHeight() {
      if (widget.switchSize == GTSwitchSizeV3.medium) {
        return 16;
      } else {
        return 24;
      }
    }

    double getToggleSize() {
      if (widget.switchSize == GTSwitchSizeV3.medium) {
        return 14;
      } else {
        return 22;
      }
    }

    double getSwitchPadding() {
      if (widget.switchSize == GTSwitchSizeV3.medium) {
        return 1;
      } else {
        return 2;
      }
    }

    return Align(
      alignment: Alignment.topLeft,
      child: FlutterSwitch(
        width: getSwitchWidth(),
        height: getSwitchHeight(),
        toggleSize: getToggleSize(),
        padding: getSwitchPadding(),
        activeColor: getActiveColor(),
        inactiveColor: getInactiveColor(),
        toggleColor: getToggleColor(),
        value: _switchValue,
        onToggle: (value) {
          if (widget.switchEnabled == true) {
            setState(() {
              _switchValue = value;
            });
            widget.onChanged?.call(value);
          }
        },
      ),
    );
  }
}
